package yuan.deng.controller;

import org.apache.commons.beanutils.BeanUtils;
import yuan.deng.domain.User;
import yuan.deng.service.UserService;
import yuan.deng.service.impl.UserServiceImpl;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    private UserService userservice=new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("findAll".equals(method)) {
            findAll(req,resp);
        }
        if ("add".equals(method)) {
            userAdd(req,resp);
        }
        if ("del".equals(method)) {
            del(req,resp);
        }
        if ("update".equals(method)) {
            update(req,resp);
        }
        if ("findById".equals(method)) {
            findById(req,resp);
        }
    }
    //查询所有
    protected void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<User> list=userservice.findAll();
        req.setAttribute("list",list);
        req.getRequestDispatcher("/pages/list.jsp").forward(req,resp);
    }
    //添加用户
    protected void userAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        String id = UUID.randomUUID().toString().replace("-", " ");
        User user=new User();
        user.setId(id);
        try {
            BeanUtils.populate(user,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        userservice.userAdd(user);
        findAll(req,resp);
    }
    //删除用户
    protected void del(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        userservice.del(id);
        findAll(req,resp);
    }
    //修改用户
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        User user=new User();
        try {
            BeanUtils.populate(user,map);

        } catch (Exception e) {
            e.printStackTrace();
        }
        userservice.update(user);
        findAll(req,resp);
    }
    //查询用户
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        User user =userservice.findById(id);
        req.setAttribute("user",user);
        req.getRequestDispatcher("/pages/update.jsp").forward(req,resp);

    }
}
